package com.xhh.stu.service;

import java.util.List;

import com.xhh.stu.activity.Activity;
import com.xhh.stu.activity.impl.ActDiscount;
import com.xhh.stu.activity.impl.ActEnoughFree;
import com.xhh.stu.cal.Calculate;
import com.xhh.stu.cal.factory.CalculateFactory;
import com.xhh.stu.entity.Goods;
import com.xhh.stu.entity.Invoice;
import com.xhh.stu.money.Money;
import com.xhh.stu.utils.RegUtils;

/**
 * 
 * <p>
 * 2017年1月4日 下午4:33:28
 * </p>
 * 
 * @author lijian
 * @version 1.0
 */
public class GoodsService {

	public Goods getMoney(String name, String unit, double qty, double price,
			String actCode,String barCode) {
		Activity act = null;
		//条形码验证通过,
		if(RegUtils.match(actCode, barCode)){
			switch (actCode) {
			case "1010":
				act = new ActDiscount(0.8);
				break;
			case "1020":
				act = new ActEnoughFree(100, 10);
				break;

			default:
				break;
			}
		}
		Calculate mulCal=CalculateFactory.getCalCulateInstance("*");
		Calculate subCal=CalculateFactory.getCalCulateInstance("-");
		Money money = new Money(act);
		double totalPrice = money.getMoney(mulCal.calculate(qty , price));
		return new Goods(name, price, qty, totalPrice, actCode, unit, subCal.calculate(mulCal.calculate(qty , price) , totalPrice),barCode);
	}

	public Invoice getInvoice(List<Goods> goods) {
		double totalPrice = 0d;
		Calculate cal = CalculateFactory.getCalCulateInstance("+");
		for (Goods g : goods) {
			totalPrice = cal.calculate(totalPrice, g.getTotalPrice());
		}
		return new Invoice(goods, totalPrice);
	}

}
